﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Manager_System
{
    public partial class order_modify : Form
    {
        string mission = "";
        string Material = "";
        main_form form1;
        public order_modify(string id, string sum, string sign_time, string request_time,  string weight, string front_money, string status, string material, string specification, string price,string start_time,string end_time,main_form form)
        {
            InitializeComponent();
            form1 = form;
            this.id_tb.Text = id;
            this.sign_time_dt.Value = Convert.ToDateTime(sign_time);
            this.sum_tb.Text = sum;
            this.request_time_dt.Value = Convert.ToDateTime(request_time);
            this.Material = material;
            this.weight_tb.Text = weight;
            this.front_money_tb.Text = front_money;
            this.price_tb.Text = price;
            this.specification_tb.Text = specification;
            if (start_time.Equals("没确定"))
            {
                start_time_dt.Value = Convert.ToDateTime("1900 - 01 - 01");
            }
            else
            {
                this.start_time_dt.Value =  Convert.ToDateTime(start_time);
            }
            if (end_time.Equals("没确定"))
            {
                start_time_dt.Value = Convert.ToDateTime("1900 - 01 - 01");
            }
            else
            {
                this.end_time_dt.Value = Convert.ToDateTime(end_time);
            }
            if (status.Equals("未开始"))
            {
                status_cb.SelectedIndex = 0;
            }
            else if (status.Equals("正生产"))
            {
                status_cb.SelectedIndex = 1;
            }
            else if (status.Equals("已完工"))
            {
                status_cb.SelectedIndex = 2;
            }
            this.modify_pl.Visible = true;
            mission = "修改";
            
        }
        public order_modify(string key,main_form form)
        {
           
            if (!key.Equals("add"))
            {
                this.Close();
            } 
            InitializeComponent();
            mission = "添加";
            this.modify_pl.Visible = false;
            form1 = form;
        }

        private void order_modify_Load(object sender, EventArgs e)
        {
            this.confirm_bt.Text = mission.ToString();

            if (this.material_cb.Items.Count == 0)
            {
                int index = 0;
                string sql = "select NAME from T_material";
                DBhelp db = new DBhelp();
                SqlDataReader reader = db.query(sql);
                while (reader.Read())
                {

                    material_cb.Items.Add(reader["NAME"].ToString());
                    if (reader["NAME"].ToString().Equals(Material.ToString()))
                    {
                        material_cb.SelectedIndex = index;
                    }
                    index++;
                }
                db.close();
                
            }
        }

        private void material_cb_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (mission.Equals("添加"))//任务是插入
            {
                sql = "insert into T_order (Sign_time,Request_time,Material,Specification,Price,Weight,Front_money,Status,Sum) values ('" + this.sign_time_dt.Value.ToString().Substring(0, 9) + "','" + this.request_time_dt.Value.ToString().Substring(0, 9) + "','" + this.material_cb.SelectedItem.ToString() + "','" + this.specification_tb.Text + "'," + price_tb.Text + "," + weight_tb.Text + "," + front_money_tb.Text + ",'" + status_cb.SelectedItem.ToString() + "'," + sum_tb.Text + ")";           
            }
            if (mission.Equals("修改"))//修改
            {
                sql = "update T_order set Sign_time='" + this.sign_time_dt.Value.ToString().Substring(0, 9) + "',Request_time='" + this.request_time_dt.Value.ToString().Substring(0, 9) + "',Material='" + this.material_cb.SelectedItem.ToString() + "',Specification='" + this.specification_tb.Text + "',Price=" + price_tb.Text + ",Weight=" + weight_tb.Text + ",Front_money=" + front_money_tb.Text + ",Status='" + status_cb.SelectedItem.ToString() + "',Sum=" + sum_tb.Text + ",Start_time='" + this.start_time_dt.Value.ToString().Substring(0, 9) + "',End_time='" + this.end_time_dt.Value.ToString().Substring(0, 9) + "' where ID=" + id_tb.Text + "";
            }
            DBhelp db = new DBhelp();
            int rs = db.update(sql);
            db.close();
            if (rs == 1)
            {
                MessageBox.Show(mission.ToString()+"成功！");
                form1.Refresh_order_LV();
            }
            else
            {
                MessageBox.Show(mission.ToString() + "失败！");

            }
            
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
